package ecom.beans;

import ecom.beans.exception.AccountBeanException;
import java.io.Serializable;
import java.util.Set;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name = "account")
@NamedQueries({
    @NamedQuery(name = "AccountBean.findAll", query = "SELECT a FROM AccountBean a"),
    @NamedQuery(name = "AccountBean.findByIban", query = "SELECT a FROM AccountBean a WHERE a.iban = :iban")})
public class AccountBean implements Serializable, AccountRemote {

    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "iban", nullable = false, length = 34)
    private String iban;
    @Basic(optional = false)
    @Column(name = "balance", nullable = false)
    private Double balance;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "iban")
    private Set<ProductstoreBean> productstoreBeanSet;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "accountBean")
    private Set<PurchaseBean> purchaseBeanSet;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "account")
    private Set<UserBean> userBeanSet;

    public AccountBean() {
    }

    public AccountBean(String iban) {
        this.iban = iban;
    }

    public AccountBean(String iban, Double balance) {
        this.iban = iban;
        this.balance = balance;
    }

    @Override
    public String getIban() {
        return iban;
    }

    public void setIban(String iban) {
        this.iban = iban;
    }

    @Override
    public Double getBalance() {
        return balance;
    }

    public void setBalance(Double balance) {
        this.balance = balance;
    }

    public Set<ProductstoreBean> getProductstoreBeanSet() {
        return productstoreBeanSet;
    }

    public void setProductstoreBeanSet(Set<ProductstoreBean> productstoreBeanSet) {
        this.productstoreBeanSet = productstoreBeanSet;
    }

    public Set<PurchaseBean> getPurchaseBeanSet() {
        return purchaseBeanSet;
    }

    public void setPurchaseBeanSet(Set<PurchaseBean> purchaseBeanSet) {
        this.purchaseBeanSet = purchaseBeanSet;
    }

    public Set<UserBean> getUserBeanSet() {
        return userBeanSet;
    }

    public void setUserBeanSet(Set<UserBean> userBeanSet) {
        this.userBeanSet = userBeanSet;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (iban != null ? iban.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        if (!(object instanceof AccountBean)) {
            return false;
        }
        AccountBean other = (AccountBean) object;
        if ((this.iban == null && other.iban != null) || (this.iban != null && !this.iban.equals(other.iban))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "[iban : " + getIban() + " balance : " + getBalance() + "]";
    }

    @Override
    public void deposit(Double amount) {
        balance = balance + amount;
    }

    @Override
    public Double withdraw(Double amount) throws AccountBeanException {
        if (balance >= amount) {
            balance = balance - amount;
        } else {
            throw new AccountBeanException("Votre solde de compte est insuffisant");
        }
        return balance;
    }
}
